﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace RegisterPage
{
    public class PurviewPageDAL
    {
        /// <summary>
        /// 写入数据库
        /// </summary>
        /// <param name="list"></param>
        /// <returns>影响行</returns>
        public static int Insert(Dictionary<string, PurviewPage> dic)
        {
            int count = 0;
            var cn = new Db.DbHelper(FormMain.DbString);

            foreach (var key in dic.Keys)
            {
                var comm = cn.GetSqlStringCommond(" select count(1) from Purview_Page where path='" + key + "'");
                if (cn.ExecuteScalar(comm).ToString() == "0")
                {
                    var updatecomn = cn.GetSqlStringCommond("insert into Purview_Page values('" + dic[key].PwId + "','" + dic[key].Name + "','" + dic[key].Path + "'," + dic[key].IsOpen.GetHashCode() + "," + dic[key].Type.GetHashCode() + ",0,'','" + "".ExtNewGuid() + "')");
                    count += cn.ExecuteNonQuery(updatecomn);
                }
            }

            return count;
        }


        public static void Update(PurviewPage page)
        {

            var cn = new Db.DbHelper(FormMain.DbString);

            var updatecomn = cn.GetSqlStringCommond("update Purview_Page  set  Name='" + page.Name + "' ,isOpen=1, PurviewEnum=" + page.PurviewEnum.GetHashCode() + " ,Detailed='" + page.Detailed + "' where PwId='" + page.PwId + "' ");
              cn.ExecuteNonQuery(updatecomn);
        }


        public static IList<PurviewPage> Select()
        {
            IList<PurviewPage> list = new List<PurviewPage>();

            var cn = new Db.DbHelper(FormMain.DbString);
            var comm = cn.GetSqlStringCommond(" select * from Purview_Page  ");
            DataTable table = cn.ExecuteDataTable(comm);

            for (int i = 0; i < table.Rows.Count; i++)
            {
                PurviewPage page = new PurviewPage
                {
                    PwId = table.Rows[i]["PwId"].ToString(),
                    Detailed = table.Rows[i]["Detailed"].ToString(),
                    IsOpen = (yq.Enums.BooleanType)int.Parse(table.Rows[i]["IsOpen"].ToString()),
                    Name = table.Rows[i]["Name"].ToString(),
                    Path = table.Rows[i]["Path"].ToString(),
                    PurviewEnum = (yq.Enums.PurviewPage.PurviewEnum)int.Parse(table.Rows[i]["PurviewEnum"].ToString()),
                    Type = (yq.Enums.PurviewPage.Type)int.Parse(table.Rows[i]["Type"].ToString()),
                };
                list.Add(page);

            }

            return list;
        }

    }
}
